package com.jia.together.utils;

import com.alibaba.fastjson.JSONArray;
import com.jia.together.entity.User;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class JwtUtil {

    public static final String SUBJECT = "jia_project";
    public static final String TOKEN_HEADER = "token";
    public static final String TOKEN_PREFIX = "Bearer ";


    public static final String SECRET_KEY= "ALIHUHDKJjaiilkld2301_192931-13da";
    public static final long EXCEPTION = 1000 * 60 * 60 * 24 * 2;
    public static final String ROLE_CLAIMS= "roles";


    public static String createJwtTokenByUser(User user) {
        String token = Jwts.builder()
                .setSubject(SUBJECT)
                .claim(ROLE_CLAIMS, user.getAuthorities())
                .claim("username", user.getUsername())
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXCEPTION))
                .signWith(SignatureAlgorithm.HS256, SECRET_KEY)
                .compact();
        return token;
    }

    public static String createJwtToken(Map<String,Object> map,long expMinuteTime) {
        long time = 1000 * 60 * expMinuteTime;
        String token = Jwts.builder()
                .setSubject(SUBJECT)
                .setClaims(map)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + time))
                .signWith(SignatureAlgorithm.HS256, SECRET_KEY)
                .compact();
        return token;
    }

    public static String createJwtToken(Map<String,Object> map) {
        long time = 1000 * 60 * 60;
        String token = Jwts.builder()
                .setSubject(SUBJECT)
                .setClaims(map)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + time))
                .signWith(SignatureAlgorithm.HS256, SECRET_KEY)
                .compact();
        return token;
    }

    public static Claims getJwtClaims(String token) {
        Claims claims = Jwts.parser()
                .setSigningKey(SECRET_KEY)
                .parseClaimsJws(token).getBody();
        return claims;
    }

    public static String getUsername(String token) {
        Claims claims = Jwts.parser()
                .setSigningKey(SECRET_KEY)
                .parseClaimsJws(token).getBody();
        return claims.get("username").toString();
    }

    public static List<SimpleGrantedAuthority> getUserRoles(String token) {
        Claims claims = Jwts.parser()
                .setSigningKey(SECRET_KEY)
                .parseClaimsJws(token)
                .getBody();
        List roles = (List) claims.get(ROLE_CLAIMS);
        String json = JSONArray.toJSONString(roles);
        List<SimpleGrantedAuthority> grantedAuthorities = JSONArray.parseArray(json,SimpleGrantedAuthority.class);
        return grantedAuthorities;
    }

    public static boolean isExpiration(String token){
        Claims claims = Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token).getBody();
        return claims.getExpiration().before(new Date());
    }

}
